// 1. createRouter 方法创建一个router实例对象  类似于new Router
// 2. createWebHashHistory  方法用来创建hash模式的路由实例

import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Category from '@/views/Category'
import Home from '@/views/Home'
import Detail from '@/views/Detail'
import Cart from '@/views/Cart'
import Settled from '@/views/Settled'
import Pay from '@/views/Pay'
import PayResult from '@/views/PayResult'

// 配置路由对应关系  本质 path - component
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '', // 当二级路由path置空的时候将作为默认的二级路由渲染
        component: Home
      },
      {
        path: 'category/:id',
        component: Category
      },
      {
        path: 'detail/:id',
        component: Detail
      },
      {
        path: 'cart',
        component: Cart
      },
      {
        path: 'settled',
        component: Settled
      },
      {
        path: 'pay',
        component: Pay
      },
      {
        path: 'pay/callback',
        component: PayResult
      }
    ]
  },
  {
    path: '/login',
    component: Login
  }
]

const router = createRouter({
  history: createWebHashHistory(), // hash模式路由
  routes,
  scrollBehavior (to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  }
})

export default router
